package com.qf.ex;

import com.qf.pickname.ex.StudentException;
import com.qf.pickname.vo.ResultVo;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 这个类作为全局异常处理器
 */
@RestControllerAdvice
public class MyExceptionHandler {


    @ExceptionHandler(StudentException.class)
    public ResultVo studentExHandle(StudentException e) {
        ResultVo resultVo = new ResultVo();
        resultVo.setCode(e.getStuExCode());
        resultVo.setMsg(e.getStuExMsg());
        resultVo.setData(e.getStuExDetail());

        return resultVo;
    }


    // 这个注解,代表该方法是一个异常处理方法,会处理 Exception 类型以及它的子类的异常
    @ExceptionHandler(Exception.class)
    public ResultVo exhandler(Exception e) {

        System.out.println(e.getMessage());
        ResultVo resultVo = new ResultVo(-1000, "服务器接口暂时无法访问,请稍后再试");
        resultVo.setData(e.getMessage());
        return resultVo;
    }

}
